*---------------------------------------------------------------------------------------------------------------;
* DESCRIPTION:		Calculating emigration year, return year, duration abroad etc. from migration data		   	;
* PROGRAMMED BY:	Mette Foged																				   	;
* CREATED:			May 2013																			   		;
* LAST MODIFIED:	May 2022																			   		;
*---------------------------------------------------------------------------------------------------------------;


dm 'cle log';
dm 'cle out';

libname grund 'H:\Rawdata\707455\Grunddata';
libname raw 'H:\Rawdata\707455\Views';
libname migdata 'L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input';

%let outputdata = L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input;



proc format;
	value 		ctr	5001   = 	'Unknown'
					5102   =	'Unknown'
					5800   =	'Unknown'
					5999   =	'Unknown'
					5103   =	'Stateless'/*Palestine refugees will belong here*/
					5199   =	'Europe'/*Unknown*/
					5299   = 	'Africa'/*Unknown*/
					5397   = 	'North America'/*Unknown*/
					5398   = 	'South America'/*Unknown*/ 
					5499   =	'Asia' /* Unknown*/
					5599   =	'Pacific Islands' /*Unknown*/
					5107   =	'Liechtenstein'
					5281   =	'Republic of Benin'
					5282   = 	'Ivory Coast'
					5283   =	'Gabon'
					5285   = 	'Niger'
					5364   =	'Paraguay'
					5366   = 	'Peru'
					5392   =	'Venezuela'
					5395   =	'Virgin Islands'
					5457   = 	'Republic of Maldives'
					5462   = 	'Oman'
					5390   = 	'United States' 
					5170   = 	'United Kingdom'/*GB*/
					5111   =	'United Kingdom'/*Nordirland*/ 
					5110   = 	'Norway' 
					5120   = 	'Sweden' 
					5180   = 	'Germany'
					5184   = 	'Germany'
					5160   = 	'Switzerland' 
					5502   = 	'Australia' 
					5505   =	'Tonga'
					5508   =	'Fiji'
					5130   = 	'France' 
					5122   = 	'Albania' 
					5128   = 	'Bulgaria' 
					5202   = 	'Algeria'
					5204   =	'Angola'
					5140   = 	'Netherlands'
					5124   = 	'Andorra'
					5402   =	'Yemen'
					5403   = 	'United Arab Emirates' /*Independent from 1971*/
					5535   =	'United Arab Emirates'/*Abu Dhabi*/
					5531   =	'United Arab Emirates'/*Dubai*/
					5534   =	'Papua New Guinea'
					5408   =	'Bhutan'
					5302   = 	'Argentina' 
					5303   =	'Bahamas'
					5182   = 	'Austria'
					5126   = 	'Belgium'
					5306   = 	'Brazil'
					5406   = 	'Bahrain'
					5410   = 	'Bangladesh'
					5305   = 	'Barbados, WI'
					5164   = 	'Spain'
					5418   = 	'Sri Lanka'
					5262   = 	'South Africa'
					5274   =	'Kirbati'
					5275   =	'Vanuatu'
					5276   =	'Central African Rep.'
					5277   =	'Cameroon'
					5278   =	'Zaire'	
					5279   =	'Congo'
					5287   =	'Rwanda'
					5484   = 	'Republic of Korea'/*South Korea*/
					5424   = 	'Taiwan'
					5266   = 	'Tanzania'
					5487   =	'Middle East unknown'
					5492   = 	'Thailand'
					5289   = 	'Somalia'
					5292   =	'Chad'
					5293   = 	'Togolese Republic'
					5324   = 	'Cuba'
					5326   =	'Dominican Republic'
					5255   = 	'Sierre Leone'
					5328   =	'Ecuador'
					5215   =	'Comoros'
					5216   =	'Eritrea'
					5372   =	'El Salvador'
					5376   =	'Uruguay'
 					5374   = 	'The Republic of Trinidad and Tobago'
					5172   = 	'Turkey'
					5269   = 	'The Republic of Uganda'
					5174   = 	'Hungary'
					5466   = 	"Korea Dem. People's Rep."/*North Korea*/
					5468   = 	'Vietnam'
					5488   = 	'Vietnam'
					5296   = 	'The Republic of Zambia'
					5295   = 	'The Republic of Zimbabwe'
					5301   = 	'Aruba'
					5311   =	'St. Vincent/Grenadin'
					5314   = 	'Canada'
					5422   = 	'Cyprus'
					5522   =	'Samoa'
					5525   =	'Djibouti'
					5526   =	'Belize CA'
					5304   = 	'The Republic of Bolivia'
					5308   =	'Guyana'
					5207   = 	'The Republic of Botswana'
					5294   = 	'Burkina Faso'
					5404   = 	'Afghanistan'
					5412   =	'Brunei'
					5414   = 	'Union of Myanmar (Burma)'
					5416   = 	'Cambodia'
					5100   = 	'Denmark'
					5318   = 	'Colombia'
					5342   =	'Haiti'
					5344   =	'Surinam'
					5345   = 	'The Commonwealth of Dominica'
					5347   =	'Saint Lucia'
					5348   =	'Honduras'
					5322   = 	'Costa Rica'
					5272   = 	'Egypt'
					5230   = 	'The Republic of Equatorial Guinea'
					5231   =	'Guinea-Bissau'
					5232   =	'Guinea'
					5233   =	'Cape Verde'
					5235   =	'Lesotho'
					5236   =	'Liberia'
					5238   =	'Libya'
					5214   = 	'Ethiopia'
					5902   = 	'The Faroe Islands'
					5474   = 	'The Philippines'
					5104   = 	'Finland'
					5105   =	'Iceland'
					5106   = 	'Iceland'
					5448   = 	'China'
					5134   = 	'Greece'
					5101   = 	'Greenland'
					5213   =	'Burundi'
					5222   =	'Gambia'
					5228   =	'The Republic of Ghana'
					5258   =	'Sudan'
					5259   =	'Swaziland'
					5338   = 	'Guatemala'
					5339   =	'Grenada'
					5428   = 	'Hong Kong'
					5142   = 	'Ireland'
					5442   = 	'Israel'
					5159   =	'San Marino'
					5150   = 	'Italy'
					5432   =	'India'
					5434   = 	'Indonesia'
					5436   =  	'Iraq'
					5438   = 	'Iran'
					5446   = 	'Jordan'
					5452   = 	'Kuwait'
					5454   =	'Lao People Dem. Rep.'
					5486   =	'Syria'
					5444   = 	'Japan'
					5234   = 	'The Republic of Kenya'
					5456   = 	'Lebanon'
					5621   =	'Sao Tome and Princip'
					5623   =	'Solomon Islands'
					5625   =	'St Kitts and Nevis'
					5108   = 	'Luxembourg' 
					5458   = 	'Malaysia'
					5297   = 	'The Republic of Malawi'
					5298   =	'Seychelles'
					5153   = 	'Malta'
					5242   =	'Madagascar'
					5243   =	'Mali'
					5244   = 	'Morocco'
					5284   = 	'Mauritania'
					5288   =	'Senegal'
					5245   = 	'Mauritius'
					5352   =	'Jamaica'
					5354   = 	'Mexico'
					5109   = 	'Monaco'
					5459   = 	'Mongolia'
					5754   =	'Ex-Yugoslavia excl. Slovenia' /*Bosnia-Herzegovina*/
					5750   =	'Ex-Yugoslavia excl. Slovenia' /*Croatia*/
					5756   =	'Ex-Yugoslavia excl. Slovenia' /*Macedonia from 1991*/
					5759   =	'Ex-Yugoslavia excl. Slovenia' /*Montenegro before WW1 and from June 2006*/
					5757   =	'Ex-Yugoslavia excl. Slovenia' /*Serbia before WW1 and after 2006*/
					5151   = 	'Ex-Yugoslavia excl. Slovenia' /*Serbia-Montenegro*/
					5752   =	'Slovenia' /*part of Ex-Yugoslavia, but should be able to separable from rest of Ex-Yugoslavia when defining EU-expansion countries*/
					5152   =  	'Ex-Yugoslavia excl. Slovenia' /*Yugoslavia*/
					5758   =    'Ex-Yugoslavia excl. Slovenia' /*Federal Republic of Yugoslavia*/
					5761   =    'Ex-Yugoslavia excl. Slovenia' /*Kosovo*/
					5129   =	'Former Czechoslovakia'/*Czechoslovakia*/
					5776   = 	'Former Czechoslovakia'/*Czech Republic*/
					5778   = 	'Former Czechoslovakia'/*Slovakia*/
					5240   = 	'Mozambique'
					5514   = 	'New Zealand'
					5247   = 	'Namibia'
					5464   = 	'Nepal'
					5356   = 	'Nicaragua'
					5246   = 	'Nigeria'
					5268   = 	'Tunisia'
					5472   = 	'Pakistan'
					5358   = 	'Panama'
					5316   = 	'Republic of Chile'
					5154   = 	'Poland'
					5156   = 	'Portugal'
					5496   = 	'Qatar'
					5478   	= 	'Saudi Arabia'
					5482   	= 	'Singapore'
					5158   	= 	'Romania'
					5706	=	'Belarus' 					/*SSR*/
					5708	=	'Armenia' 					/*SSR*/
					5162   = 	'Soviet Union' 				/*SSR*/
					5700   	= 	'Russia' 					/*SSR*/
					5704   = 	'Ukraine'					/*SSR*/
					5609   =	'Latvia'					/*SSR*/
					5611   = 	'Lithuania'					/*SSR*/
					5607   = 	'Estonia'					/*SSR*/
					5710   =	'Azerbaijan' 				/*SSR*/
					5714   = 	'Uzbekistan'				/*SSR*/
					5712   =	'Republic of Moldova'		/*SSR*/
					5716   = 	'The Republic of Kazakhstan'/*SSR*/
					5720	=	'Kyrgyzstan'				/*SSR*/
					5722    =	'Tadzhikstan' 				/*SSR*/
					5724    =	'Georgia'					/*SSR*/
					5718    =	'Turkmenistan' 				/*SSR*/;
run;

* Load migration register;
data temp1 (drop = pnrx);
 	set raw.vndsv (keep = pnr haend_dato indud_kode indud_land rename=(pnr=pnrx haend_dato=dato));
	pnr=pnrx+0;
run;

proc sort data=temp1;
	by pnr descending dato;
run;


* Calculate duration of spell;
data temp2 (drop=dato1 dato2); 
	set temp1;
	retain dato1;
	by pnr;	
	if first.pnr then durat = .;
		
	else do;
		dato2 = dato;
		durat = datdif(dato2,dato1,'30/360');
	end;
	year = year(dato);
	output;
	dato1 = dato;
run;


*-----------------------------------   DATA WITH ALL MIGRATIONS   ----------------------------------------------;


data in_and_out; 
	set temp2;
	label	durat 		=	"Length of stay"
			year 		= 	"Year of spell start"
			dato 		= 	"Date of spell start"
			indud_kode = 	"I immigration, U emigration"
			indud_land =	"Sending/destination country";
			
run;


*---------------------------------------   EMIGRATION DATA   -------------------------------------------------;


data emigrations (drop = indud_kode); 
	set in_and_out (rename=(indud_land=udv_land));
	where indud_kode = "U";
	label 	udv_land 	= 	"Destination country";
	format udv_land ctr.;
run;

proc sort data=emigrations nodupkey; by pnr dato; 




*---------------------------------------   IMMIGRATION DATA   ------------------------------------------------;


data immigrations (drop = indud_kode); 
	set in_and_out (rename=(indud_land=indv_land));
	by pnr;
	where indud_kode = "I";
	first_immi=0; if last.pnr then first_immi=1;
	label 	first_immi	=	"First time immigration" 
			dato		=	"Date of entry"
			year		=	"Year of entry"
			indv_land	= 	"Sending country";
	format indv_land ctr.;
run;

proc sort data=immigrations nodupkey; by pnr dato; 


proc export data=immigrations outfile="&outputdata.\Immigrations.dta" replace; run;


*-------------------------------------------------------------------------------------------------------------;


* CLEAR ;
proc datasets lib=work kill memtype=data;
title "";
run;
quit;


